//
//  DAWebFileViewController.m
//  DAKnowledge
//
//  Created by DACadmin on 16/7/19.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAWebFileViewController.h"
#import <WebKit/WebKit.h>
@interface DAWebFileViewController ()<WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *fileView;
@end

@implementation DAWebFileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.fileName);
    _fileView= [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 55)];
    _fileView.navigationDelegate = self;
    self.navigationItem.title = self.fileName;
    //NSString *str = @"http://10.2.3.34/DAKnowledgeUpload/uploads/%E8%BF%99%E5%9B%9E%E6%9C%89%E4%BA%86222.png";
   // NSURL *url = [NSURL fileURLWithPath:@"10.2.3.34/DAKnowledgeUpload/uploads/%E8%BF%99%E5%9B%9E%E6%9C%89%E4%BA%86222.png"];//self.fileUrl];
    NSString *str = self.fileUrl;
    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)str, NULL, NULL,  kCFStringEncodingUTF8 ));
    NSURL *url = [NSURL URLWithString:encodedString];
    NSURL *url_try = [NSURL URLWithString:str];
   // NSString *filestr = [url absoluteString];
    NSLog(@"type %@",self.fileExtend);
    if ([self.fileExtend isEqualToString:@"txt"] || [self.fileExtend isEqualToString:@"TXT"]) {
        NSData *data = [NSData dataWithContentsOfURL:url];
        [_fileView loadData:data MIMEType:@"text/txt" characterEncodingName:@"Utf-8" baseURL:url_try];
    }else{
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_fileView loadRequest:request];
    }
    [self.view addSubview:self.fileView];
    // Do any additional setup after loading the view.
}
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    [SVProgressHUD showWithStatus:@"正在加载"];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{

}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [SVProgressHUD dismiss];
    
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
    [SVProgressHUD showErrorWithStatus:@"加载失败"];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidAppear:animated];
    [SVProgressHUD dismiss];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
